﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class main : System.Web.UI.MasterPage
{
    BLL.Users blluser = new BLL.Users();
    protected BLL.Search bllsearch = new BLL.Search();
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 绑定 加载热词
        //绑定 加载热词
        if (common.getKeyWord() != null)
        {
            rpHotWords.DataSource = common.getKeyWord();
            rpHotWords.DataBind();
        }
        #endregion

      
        //判断是否访问(登录过session不为空)，是否记住我
        if (!IsPostBack)
        {
            if (!common.isLoginOrNot())
            {
                modify.Visible = false;
                logOut.Visible = false;
            }
            else
            {
                //session不为null则显示
                show();
            }
        }
    }

    #region 对控件的隐藏显示操作
    public void show()
    {
        retrieve.Visible = false;
        modify.Visible = true;
        //注意这里得用session来取id的值，不要直接有model
        modify.HRef = "modify.aspx?id=" + ((MODEL.Users)Session["currentUser"]).UId;
        logOut.Visible = true;
    }
    #endregion

    #region 退出
    /// <summary>
    /// 退出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void logOut_Click(object sender, EventArgs e)
    {
        common.LoginOut();
    }
    #endregion

    #region 提交（通过邮件重置密码）
    /// <summary>
    /// 提交（通过邮件重置密码）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void divSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string name = divUsername.Value.Trim();
            string question = divDropDL.SelectedItem.Text.Trim();
            string answer = divAnswer.Value.Trim();
            if (blluser.isExistOrNotByQA(name, question, answer))
            {
                MODEL.Users model = blluser.getModel(name);
                try
                {
                    Mail.sendMail(model.Email, name);
                    MessageBox.Show(Page, "系统为您重置的密码已经发送到您的邮箱");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Page, "发送邮件出现异常");
                    throw new Exception(ex.Message);
                }
            }
            else
            {
                MessageBox.Show(Page, "用户名不存在或密保问题有错，答案不正确");
            }
        }
    }
    #endregion

    #region 搜索
    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        //将搜索的词汇加入到数据库
        try
        {
            bllsearch.addKeyWord(txtSearch.Value);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
       
        Response.Redirect("searchResults.aspx?keyword=" + txtSearch.Value);
    }
    #endregion
}
